<?php
/**
 * $Id: controller.php 11 2010-05-18 11:39:11Z webbochsant $
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 

//Import the controller classes
jimport('joomla.application.component.controller');
 
/**
 * 
 * @author Daniel
 * @package Joomla
 *
 */
class VmcompetitionController extends JController
{
    /**
     * Method to display the view
     *
     * @access    public
     */
    function display()
    {
    	
    	JRequest::setVar('view','vmcompetition'); // force it to be the competition view
        parent::display();
    }
 	
    /**
     * Method for competing
     */
    function compete(){
    	global $mainframe;
    	
    // Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$db				=& JFactory::getDBO();
		$competition_id	= JRequest::getInt( 'compid', 			0, 			'post' );
		$name			= JRequest::getVar( 'name',			'',			'post' );
		$email			= JRequest::getVar( 'email',		'',			'post' );
		$linkurl		= JRequest::getVar( 'linkurl',		'',			'post' );
		$comment		= JRequest::getVar( 'text',			'',			'post' );
		
		
		

		//$contestant =& JTable::getInstance('contestant','Table');
		$competition =& JTable::getInstance('vmcompetition','Table');
		
		$date =& JFactory::getDate();
		
		//Check if competition is available
		if (!$competition->load( $competition_id )) {
			JError::raiseWarning( 404, JText::_('ALERTNOTAUTH') );
			return;
		}
		/*
		else if ( ($competition->publish_up > $date) || ( $competition->publish_down < $date ) ){
			JError::raiseWarning( 404, JText::_('COMPETITIONNOTACTIVE') );
			return;
		}
		*/
		
		$cookieName	= JUtility::getHash( $mainframe->getName() . 'compete' . $competition_id );
		// ToDo - may be adding those information to the session?
		$competed = JRequest::getVar( $cookieName, '0', 'COOKIE', 'INT');

		if ($competed || !$competition_id )
		{
			if($competed) {
				$msg = JText::_('You already competed for this competition today!');
			}

			if(!$competition_id){
				$msg = JText::_('WARNSELECT');
			}
		}
		else
		{
			

			require_once(JPATH_COMPONENT.DS.'models'.DS.'compete.php');
			$model = new VmcompetitionModelCompete();
			$validated = 0;
			//Validate the url provided
			if ($model->validateURL($linkurl)){
				
				//Check the link for backlinks
				if($model->checkBacklink($linkurl, "http://www.stilero.se")){
					
					//Set cookies to make sure the contestant don't apply twice
					$lagtime = 86400;	//Seconds between applies
					$lagtime = 0;	//Seconds between applies
					$validated = 1;
					setcookie( $cookieName, '1', time() + $lagtime );
					$model->compete( $competition_id, $name, $email, $linkurl, $comment, $validated );
					$msg = JText::_( 'Thanks for competing!' );
				
				}else{
					
					$msg = JText::_( 'No backlink found.' );
				
				}
				
			}else{
				
				$msg = JText::_( 'Please check your URL, something is not right.' );
			
			}
			
			//Set the messagetype. Error if link is not validated, else just a message.
			$messagetype = ($validated==0)?"error":"";
			
			//repopulate the form with the values just entered.
			//JRequest::setVar('namnet', $name);
			/*
			JRequest::AssignREf('name', $name);
			JRequest::AssignREf('email', $email);
			JRequest::AssignREf('linkurl', $linkurl);
			JRequest::AssignREf('comment', $comment);
			JRequest::AssignREf('competition_id', $competition_id);
			*/
			$this->setRedirect( JRoute::_('index.php?option=com_vmcompetition', false), $msg, $messagetype );
		}
		
    }
}

?>